package com.shuhua.taskServer.service.security;

import com.shuhua.pojo.entity.Craftsman;
import com.shuhua.pojo.entity.Admin;
import com.shuhua.pojo.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 通用用户详情实现，支持不同类型的用户实体
 */
@RequiredArgsConstructor
public class GenericUserDetails<T extends UserInfo> implements UserDetails {

    private final T user;
    private final Function<T, String> passwordExtractor;
    private final Function<T, Collection<? extends GrantedAuthority>> authoritiesExtractor;
    private final Supplier<Boolean> accountNonExpiredSupplier;
    private final Supplier<Boolean> accountNonLockedSupplier;
    private final Supplier<Boolean> credentialsNonExpiredSupplier;
    private final Supplier<Boolean> enabledSupplier;

    public T getUser() {
        return user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authoritiesExtractor.apply(user);
    }

    @Override
    public String getPassword() {
        return passwordExtractor.apply(user);
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpiredSupplier.get();
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLockedSupplier.get();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpiredSupplier.get();
    }

    @Override
    public boolean isEnabled() {
        return enabledSupplier.get();
    }

    // 工匠用户详情工厂方法
    public static GenericUserDetails<Craftsman> createForCraftsman(Craftsman craftsman) {
        return new GenericUserDetails<>(
                craftsman,
                Craftsman::getPassword,
                // 为工匠用户赋予 "ROLE_CRAFTSMAN" 角色
                user -> Collections.singletonList(new SimpleGrantedAuthority("ROLE_CRAFTSMAN")),
                () -> true,
                () -> true,
                () -> true,
                () -> craftsman.getAuthStatus() != null && craftsman.getAuthStatus() == 1
        );
    }

    // 管理员用户详情工厂方法
    public static GenericUserDetails<Admin> createForAdmin(Admin admin) {
        return new GenericUserDetails<>(
                admin,
                Admin::getPassword,
                user -> {
                    String role = Boolean.TRUE.equals(user.getIsSuper()) ? "ROLE_SUPER_ADMIN" : "ROLE_ADMIN";
                    return Collections.singletonList(new SimpleGrantedAuthority(role));
                },
                () -> true, // 账户未过期
                () -> Boolean.TRUE.equals(admin.getStatus()), // 账户未锁定
                () -> true, // 凭证未过期
                () -> Boolean.TRUE.equals(admin.getStatus()) // 账户启用
        );
    }
}